package com.whxd.smarthome.api.rest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.whxd.framework.common.dto.ReturnObject;
import com.whxd.framework.common.exception.BusinessException;
import com.whxd.smarthome.api.entity.Version;
import com.whxd.smarthome.api.rest.dto.VersionDto;
import com.whxd.smarthome.api.service.VersionService;
import com.whxd.smarthome.util.MediaTypes;

@RestController
@RequestMapping(value = "/api/version")
public class VersionCheckController {

	@Autowired
	private VersionService versionService;

	private static Logger logger = LoggerFactory.getLogger(DhUserRestController.class);

	@RequestMapping(value = "/android", produces = MediaTypes.JSON_UTF_8)
	@ResponseBody
	public ReturnObject android() {
		logger.info("Check android version");

		ReturnObject ro = new ReturnObject();
		try {
			ro.setSuccess(true);

			VersionDto dto = new VersionDto();

			Version androidVersion = versionService.findAndroidVersion();

			if (androidVersion == null) {
				dto.setVersion(0);
				dto.setName("null");
				dto.setPath("null");
			} else {
				dto.setVersion(androidVersion.getVersion());
				dto.setName(androidVersion.getName());
				dto.setPath(androidVersion.getPath());
			}
			
			ro.setObj(dto);
		} catch (BusinessException e) {
			ro.setSuccess(false);
			ro.setMsg(e.getMessage());
		}

		return ro;
	}
}
